package com.yanqu.road.server.manger.activity.entername.impl;

import com.yanqu.road.entity.activity.entername.ActivityGroupMatch;
import com.yanqu.road.pb.cross3.activity.EnterNameProto;
import com.yanqu.road.server.gameplayer.GamePlayer;
import com.yanqu.road.server.manger.activity.entername.EnterNameActivityConfig;
import com.yanqu.road.server.manger.activity.entername.EnterNameActivityMgr;
import com.yanqu.road.server.manger.activity.entername.EnterNameCache;
import com.yanqu.road.server.manger.player.GamePlayerMgr;
import com.yanqu.road.server.protocol.Protocol;

import java.util.List;

public class DefaultEnterNameChannelListener extends AbstractEnterNameChangeListener {

    @Override
    public void notifyGrouping(EnterNameProto.UserEnterNameMsg nameMsg, List<ActivityGroupMatch> list, EnterNameActivityConfig config) {
        GamePlayer player = GamePlayerMgr.getOnlinePlayer(nameMsg.getUserId());
        if(player == null){
            return;
        }
        EnterNameProto.EnterNameSyncMsg.Builder sync = EnterNameProto.EnterNameSyncMsg.newBuilder();
        sync.addName(nameMsg);
        player.sendPacket(Protocol.U_ENTER_NAME_SYNC, sync);
        EnterNameActivityMgr.getLogger().info("enter name activity {} , sync user {} zoneId {} groupId {}", nameMsg.getActivityId(), player.getUserId(),  nameMsg.getZoneId(), nameMsg.getGroupId());
    }

    @Override
    public void notifyName(long playerId, int activityId, EnterNameCache cache) {
        //报名通知
        GamePlayer player = GamePlayerMgr.getOnlinePlayer(playerId);
        if(player == null){
            return;
        }
        EnterNameProto.EnterNameSyncMsg.Builder sync = EnterNameProto.EnterNameSyncMsg.newBuilder();
        EnterNameProto.UserEnterNameMsg.Builder builder = EnterNameProto.UserEnterNameMsg.newBuilder();
        builder.setActivityId(activityId);
        builder.setUserId(playerId);
        builder.setZoneId(cache.getZoneId());
        sync.addName(builder);
        player.sendPacket(Protocol.U_ENTER_NAME_SYNC, sync);
        EnterNameActivityMgr.getLogger().info("enter name activity {} , sync user {} zoneId {}", activityId, player.getUserId(),  cache.getZoneId());
    }

    @Override
    public boolean supportType(int activityType) {
        return true;
    }
}
